#include "map_global.h"
#include <algorithm>

MapGlobal::MapGlobal(int width, int height, float grid_size)
    : width_(width), height_(height), size_(width * height) , grid_size_(grid_size),  map_(width * height, 128)
{
    // map_ = new unsigned char[size_];
    // std::fill(map_, map_ + size_, 128);
    // 手动初始化 map_
    // map_.resize(size_);               // 调整 vector 大小
    // std::fill(map_.begin(), map_.end(), 128); // 填充所有元素为 128

    min_x_ = int(-4.152f / grid_size_);
    min_y_ = int(-4.219f / grid_size_);
    max_x_ = int(0.545f / grid_size_);
    max_y_ = int(1.567f / grid_size_);

    zero_x_ = int(min_x_ / grid_size_);
    zero_y_ = int(min_y_ / grid_size_);
}

MapGlobal::~MapGlobal() 
{
    // delete[] map_;
}

void MapGlobal::updateCell(int x, int y, unsigned char value) 
{
    if (isValidCell(x, y)) {
        map_[y * width_ + x] = value;
    }
}

unsigned char MapGlobal::getCell(int x, int y) const 
{
    if (isValidCell(x, y)) {
        return map_[y * width_ + x];
    }
    return 255;
}


const std::vector<unsigned char>& MapGlobal::getMap() const 
{
    return map_;
}

int MapGlobal::getWidth() const
{
    return width_;
}

int MapGlobal::getHeight() const 
{
    return height_;
}

bool MapGlobal::isValidCell(int x, int y) const 
{
    return x >= 0 && x < width_ && y >= 0 && y < height_;
}


void MapGlobal::update(const std::vector<unsigned char>& local_map)
{
    // TO DO
    std::cout << "update Global Map " << std::endl;

    std::cout << "map_: " << std::endl;

}




